package com.aoxin.service.impl.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aoxin.mapper.client.EpOrderServiceDao;
import com.aoxin.mapper.client.RecentlyMadeReservationsDao;
import com.aoxin.mapper.course.systemCourse.SchoolCoursePageDao;
import com.aoxin.mapper.course.webCourse.MyCoursePageMapper;
import com.aoxin.mapper.course.webCourse.SlfChildrenMapper;
import com.aoxin.mapper.opm.SlfSchoolMapper;
import com.aoxin.service.inter.client.RecentlyMadeReservationsService;
import com.aoxin.util.DateWay;
import com.aoxin.util.ThreadLocalUtil;

/**
 *   最近预约serviceimpl
 * @author lop
 * @date 2018年5月25日 下午4:49:20
 */
@Service("RecentlyMadeReservationsService")
public class RecentlyMadeReservationsServiceImpl implements RecentlyMadeReservationsService{
	@Autowired
	private RecentlyMadeReservationsDao recentlyMadeReservationsDao;
	
	@Autowired
	private EpOrderServiceDao epOrderServiceDao;
	
	@Autowired
	private MyCoursePageMapper myCoursePageMapper;
	
	@Autowired
	private SchoolCoursePageDao schoolCoursePageDao;
	@Autowired
	SlfChildrenMapper slfchildrenmapper;
	@Autowired
	SlfSchoolMapper slfSchoolMapper;
	 
	@Override
	public List<Map<String, Object>> checkLimitRecentlyMadeReservations() {
		/**
		 *  判断用户是否存在 若存在判断信息是否完善，若完善再判断是否购买课程包 
		 *  
		 *  dataType 1 为课程  2 为课程包
		 *  courseType 1 为B类课程 2为C类课程
		 *  
		 */
		
		List<Map<String,Object>> resultMapList=new ArrayList<Map<String,Object>>();
		// 日期格式解析
		DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
		boolean childrenBoolean=true;
		if(ThreadLocalUtil.getUser()!=null){
			//获取孩子信息
		List<Integer> listChildrenId=slfchildrenmapper.getChildrenId(ThreadLocalUtil.getUser().getUserId());
		if(listChildrenId.size()>0){
			childrenBoolean=false;
		}
		}
		if(childrenBoolean|| ThreadLocalUtil.getUser()==null){
			/***
			 * 课程数据为空   
			 * 
			 * 未登陆用户
			 * 
			 * 查询一个未预约过的主题课
			 *  若有返回一个主题课并返回一个未购买热门的课程包
			 *  没有返回两个课程包 
			 *   若只有一个课程包 则返回一个课程包和一个课程
			 */
			//C类课程空位数据 2 为C类
			/*Integer innvalue=2;
			List<Map<String,Object>> courseDataCList= recentlyMadeReservationsDao.queryVacancyCourseDataCorBOrC(innvalue);*/
			String date = DateWay.GetTime();
			Map<String,Object> mapList = new HashMap<String, Object>();
			mapList.put("timeAll",DateWay.TimeAll());
			mapList.put("courseTime",date);
			List<Map<String, Object>> SlfClassroomCEmployee = myCoursePageMapper.getSlfClassroomCEmployeeToday(mapList);
			//查出教室空余大于2个位置的班级
				if(SlfClassroomCEmployee !=null && SlfClassroomCEmployee.size()>0){
					/**
					 * 主题课不为空  获取一条主题课
					 */
					for (int i = 0;i<SlfClassroomCEmployee.size();i++) {
						mapList.put("id",SlfClassroomCEmployee.get(i).get("id").toString());
						Integer count = myCoursePageMapper.getZjChildrenNumberCount(mapList);
						String a = SlfClassroomCEmployee.get(i).get("classroomNumber").toString();
						Integer b = Integer.parseInt(a);
						if(b > count-2){
						String courseStartTime=SlfClassroomCEmployee.get(i).get("courseStartTime").toString();
						String courseId = SlfClassroomCEmployee.get(i).get("courseId").toString();
						Map<String,Object> map = schoolCoursePageDao.getCourseMessage(Integer.parseInt(courseId));
						DateTime t2 = DateTime.parse(courseStartTime, format);
						Duration duration=new Duration(new DateTime(),t2);
						Long countDown =duration.getStandardSeconds();
						SlfClassroomCEmployee.get(i).put("countDown", countDown);
						map.put("id", SlfClassroomCEmployee.get(i).get("id").toString());
						map.put("name", SlfClassroomCEmployee.get(i).get("courseName").toString());
						map.put("endTime", SlfClassroomCEmployee.get(i).get("endTime").toString());
						map.put("classroomId", SlfClassroomCEmployee.get(i).get("classroomId").toString());
						map.put("classroomName", SlfClassroomCEmployee.get(i).get("classroomName").toString());
						map.put("classroomNumber", SlfClassroomCEmployee.get(i).get("classroomNumber").toString());
						map.put("courseSate",0);
						map.put("course_type",3);
						map.put("dataType", 1);
						Integer totalNumber = myCoursePageMapper.getClassroomNumber(map);
					    Integer alearyNumber = myCoursePageMapper.getZjChildrenNumberCount(map);
					    Integer syTotal = totalNumber - alearyNumber;
					    map.put("syClassroomNumber",syTotal);
						//找到schoolName
						String classroomId = SlfClassroomCEmployee.get(i).get("classroomId").toString();
						map.put("schoolName",slfSchoolMapper.getSchoolNameOther(Integer.parseInt(classroomId)));
						map.put("school_id",SlfClassroomCEmployee.get(i).get("classroomId").toString());
						map.put("course_page_id",SlfClassroomCEmployee.get(i).get("courseId").toString());
						map.put("countDown",SlfClassroomCEmployee.get(i).get("countDown").toString());
						map.put("courseTime",SlfClassroomCEmployee.get(i).get("courseTime").toString());
						resultMapList.add(map);
						break;
				 }
					}
					// 查询一个热门课程包
					List<Map<String,Object>> hotCoursePageList=recentlyMadeReservationsDao.queryHotCoursePage(null,null);
				 if(hotCoursePageList.size()>0){
					 //推荐课程包不为空
					 hotCoursePageList.get(0).put("dataType", 2);
					 hotCoursePageList.get(0).put("schoolName", "请先登录");
					 resultMapList.add(hotCoursePageList.get(0));
				 }
				 return resultMapList;
				}else{
					/**
					 * 主题课为空返回两课程包 
					 */
					 List<Map<String,Object>>	hotCoursePageList=recentlyMadeReservationsDao.queryHotCoursePage(null,null);
					if(hotCoursePageList.size()>=2){
						hotCoursePageList.get(0).put("dataType", 2);
						hotCoursePageList.get(1).put("dataType", 2);
						 hotCoursePageList.get(0).put("schoolName", "请先登录");
						 hotCoursePageList.get(1).put("schoolName", "请先登录");
						resultMapList.add(hotCoursePageList.get(0));
						resultMapList.add(hotCoursePageList.get(1));
					}else{
						if(hotCoursePageList.size()>0&&hotCoursePageList.size()<2){
							hotCoursePageList.get(0).put("dataType", 2);
							 hotCoursePageList.get(0).put("schoolName", "请先登录");
							resultMapList.add(hotCoursePageList.get(0));
						}
						/**
						 * 查询一个最近B类课程
						 */
						
						List<Map<String,Object>> courseDataBList= recentlyMadeReservationsDao.queryVacancyCourseDataCorBOrC(1);
							//推荐B类不为空
						if( courseDataBList !=null && courseDataBList.size()>0 ){
							
								//得到一个本校区 在number 表中 缺人 的最近的一条课程数据  
								Map<String,Object> recentlyMadeReservationsCourseData=recentlyMadeReservationsDao.queryCourseDetails(Integer.parseInt(courseDataBList.get(0).get("course_id").toString()),null); 	
								// 查询有空余位置教室信息  
								
								List<Map<String, Object>> freePositionData = recentlyMadeReservationsDao.queryRoomIdAndEndTime(null);
								if(freePositionData!=null&& freePositionData.size()>0){
									Map<String,Object>	 resultMapCourse=new HashMap<String,Object>();
									Integer classcourserId=0;
									Integer vacancyNumber=0;
									Integer classRoomId=0;
									Map<String,Object>freePositionDataCourse=new HashMap<String,Object>();
									for (int i = 0,b=freePositionData.size(); i < b; i++) {
										 classRoomId=Integer.parseInt(freePositionData.get(i).get("classroom_id").toString());
										//排除掉 该堂课已满的教室
										resultMapCourse = recentlyMadeReservationsDao.queryFreePositionCourse(classRoomId,freePositionData.get(i).get("employ_time").toString());
										classcourserId=Integer.parseInt(resultMapCourse.get("course_id").toString());  
										//查询空余教室课程是否在该用户学校下
										freePositionDataCourse=recentlyMadeReservationsDao.queryCourseDetails(Integer.parseInt(freePositionData.get(i).get("course_id").toString()),null); 	
										//获取该堂课程空余人数
										if(freePositionDataCourse!=null &&freePositionDataCourse.size()>0){
											Integer vacancyCourseNumber	=recentlyMadeReservationsDao.queryVacancyCourseNumber(classcourserId,classRoomId,resultMapCourse.get("employ_time").toString());
											Integer classVacancyCourseNumber=Integer.parseInt(freePositionData.get(i).get("employ_num").toString()); 
											if(classVacancyCourseNumber>vacancyCourseNumber){
												vacancyNumber=(classVacancyCourseNumber-vacancyCourseNumber);
												break;
											}
										}
									}
									
									
									//教室结束时间
									DateTime t3 = DateTime.parse(resultMapCourse.get("employ_time").toString(), format);
									//class_number
									DateTime t4 = DateTime.parse(recentlyMadeReservationsCourseData.get("end_time").toString(), format);
									if(t4.isBefore(t3)){
										//class_number 时间在前
										recentlyMadeReservationsCourseData.put("freePosition",Integer.parseInt(courseDataBList.get(0).get("vacancy").toString()));
										// 日期格式化   开课时间
										DateTime t2 = DateTime.parse(courseDataBList.get(0).get("end_time").toString(), format).minusHours(1);
										recentlyMadeReservationsCourseData=getCourseData(recentlyMadeReservationsCourseData,t2,null);
										recentlyMadeReservationsCourseData.put("classRoomId", courseDataBList.get(0).get("classroom_id"));
										recentlyMadeReservationsCourseData.put("courseType", 1);
										recentlyMadeReservationsCourseData.put("dataType", 1);
										resultMapList.add(recentlyMadeReservationsCourseData);
									
									}else{
										
										//教室那边的时间在前
									freePositionDataCourse.put("freePosition",vacancyNumber);
									// 日期格式化   开课时间
									DateTime t2 = DateTime.parse(resultMapCourse.get("employ_time").toString(), format).minusHours(1);
									freePositionDataCourse=getCourseData(freePositionDataCourse,t2,null);
									freePositionDataCourse.put("classRoomId", classRoomId);
									freePositionDataCourse.put("dataType", 1);
									freePositionDataCourse.put("courseType", 1);
									resultMapList.add(freePositionDataCourse);
									}
									
								}else{
									//class_number 时间在前
									recentlyMadeReservationsCourseData.put("freePosition",Integer.parseInt(courseDataBList.get(0).get("vacancy").toString()));
									// 日期格式化   开课时间
									DateTime t2 = DateTime.parse(courseDataBList.get(0).get("end_time").toString(), format).minusHours(1);
									recentlyMadeReservationsCourseData=getCourseData(recentlyMadeReservationsCourseData,t2,null);
									recentlyMadeReservationsCourseData.put("classRoomId", courseDataBList.get(0).get("classroom_id"));
									recentlyMadeReservationsCourseData.put("courseType", 1);
									recentlyMadeReservationsCourseData.put("dataType", 1);
									resultMapList.add(recentlyMadeReservationsCourseData);
								}
								
							}else{
								// 查询有空余位置教室信息  
								
								List<Map<String, Object>> freePositionData = recentlyMadeReservationsDao.queryRoomIdAndEndTime(null);	
								if(freePositionData.size()>0){
									Map<String,Object>	 resultMapCourse=new HashMap<String,Object>();
									Integer classcourserId=0;
									Integer vacancyNumber=0;
									Integer classRoomId=0;
									Map<String,Object>freePositionDataCourse=new HashMap<String,Object>();
									for (int i = 0,b=freePositionData.size(); i < b; i++) {
										 classRoomId=Integer.parseInt(freePositionData.get(i).get("classroom_id").toString());
										//排除掉 该堂课已满的教室
										resultMapCourse = recentlyMadeReservationsDao.queryFreePositionCourse(classRoomId,freePositionData.get(i).get("employ_time").toString());
										classcourserId=Integer.parseInt(resultMapCourse.get("course_id").toString());  
										//查询空余教室课程是否在该用户学校下
										freePositionDataCourse=recentlyMadeReservationsDao.queryCourseDetails(Integer.parseInt(freePositionData.get(i).get("course_id").toString()),null); 	
										//获取该堂课程空余人数
										if(freePositionDataCourse!=null &&freePositionDataCourse.size()>0){
											Integer vacancyCourseNumber	=recentlyMadeReservationsDao.queryVacancyCourseNumber(classcourserId,classRoomId,resultMapCourse.get("employ_time").toString());
											Integer classVacancyCourseNumber=Integer.parseInt(freePositionData.get(i).get("employ_num").toString()); 
											if(classVacancyCourseNumber>vacancyCourseNumber){
												vacancyNumber=(classVacancyCourseNumber-vacancyCourseNumber);
												break;
											}
										}
									}
									//教室结束时间
									DateTime t3 = DateTime.parse(resultMapCourse.get("employ_time").toString(), format);
									//教室那边的时间在前
									freePositionDataCourse.put("freePosition",vacancyNumber);
									// 日期格式化   开课时间
									DateTime t2 = DateTime.parse(resultMapCourse.get("employ_time").toString(), format).minusHours(1);
									freePositionDataCourse=getCourseData(freePositionDataCourse,t2,null);
									freePositionDataCourse.put("classRoomId", classRoomId);
									freePositionDataCourse.put("dataType", 1);
									freePositionDataCourse.put("courseType", 1);
									resultMapList.add(freePositionDataCourse);	
								}
							}
						
					}
					return resultMapList;
				}
				
		}else{
			String schoolName= ThreadLocalUtil.getChildren().getSchoolName();
			
			/**
			 * 信息完善 且登陆用户
			 */
			Integer childrenId = ThreadLocalUtil.getChildren().getChildrenId();
			Integer schoolId = ThreadLocalUtil.getChildren().getSchoolId();
			//条件时间查询
			List<Map<String, Object>> stringObjectMap =this.recentlyMadeReservationsDao.checkLimitCourse(childrenId);
			if (stringObjectMap != null && stringObjectMap.size()>0) {
				// 已经进行中的B类课程
				// 如果判断时间 如果大于4小时
				List<Map<String, Object>> latestCourse = recentlyMadeReservationsDao.queryLatestCourse(childrenId,null);
				int c=0;
				if(latestCourse.size()>2){
					c=2;
				}else{
					c=latestCourse.size();
				}
				for (int i = 0,b=c; i < b; i++) {
					if(Integer.parseInt(latestCourse.get(i).get("employ_flag").toString())==1 ){
						continue;
					}
					// 排表时间
					DateTime t2 = DateTime.parse(latestCourse.get(i).get("employ_time").toString(), format);
					// 如果小于或者等于4小时
					DateTime t1 = new DateTime().plusHours(4);
					Map<String, Object> courseData = recentlyMadeReservationsDao
							.queryCourseDetails(Integer.parseInt(latestCourse.get(i).get("course_id").toString()), schoolId);
					
					courseData.put("id", latestCourse.get(i).get("id"));	
					//查询学校课程包ID
					Map<String,Object> schoolCoursetoPageData=recentlyMadeReservationsDao.querySchoolCoursePageId(Integer.parseInt(courseData.get("school_course_id").toString()), schoolId);
					courseData.put("school_course_page_id",schoolCoursetoPageData.get("school_course_page_id"));
					if (Integer.parseInt(latestCourse.get(i).get("Flag").toString()) == 2) {
						// 2 为排队中
						courseData.put("courseSate", "2");
					} else {
						if (t2.isAfter(t1)) {
							//startDate 选择时间 默认为大于4小时
							// 大于4个小时 3按钮为取消
							courseData.put("courseSate", "3");
						} else {
							// 小于4个小时 即将开始
							courseData.put("courseSate", "4");
						}
					}
					Duration duration=new Duration(t1,t2);
					Long countDown =duration.getStandardSeconds();
					courseData.put("countDown", countDown);
					//1 为课程
					courseData.put("dataType", "1");
					courseData.put("courseType", 1);
					if(courseData.get("sales_start_date")!=null && courseData.get("sales_start_date")!=""){
						String  str=courseData.get("sales_start_date").toString();
						courseData.put("sales_start_date", new DateTime(str).toString("yyyy-MM-dd")); 
					}
					if(courseData.get("sales_end_date")!=null && courseData.get("sales_end_date")!=""){
						String  str=courseData.get("sales_end_date").toString();
						courseData.put("sales_end_date", new DateTime(str).toString("yyyy-MM-dd")); 
					}
					Integer classRoomId=Integer.parseInt(latestCourse.get(i).get("classrooom_id").toString());
					String  classRoomName=epOrderServiceDao.queryClassRoom(classRoomId);
					courseData.put("classRoomName", classRoomName);
					courseData.put("schoolName", schoolName);	
					//TODO 时间
					String startTime=t2.minusHours(1).toString("yyyy-MM-dd hh:mm:ss");
					String endTime=t2.toString("hh:mm:ss");
					courseData.put("courseTime", startTime+"-"+endTime);
					
					resultMapList.add(courseData);
				}
				return resultMapList;
			}else{
				/***
				 * 课程数据为空   
				 * 
				 * 查询一个未预约过的主题课
				 *  若有返回一个主题课并返回一个未购买热门的课程包
				 *  没有返回两个课程包 
				 *   若只有一个课程包 则返回一个课程包和一个课程
				 */
				String date = DateWay.GetTime();
				Map<String,Object> mapList = new HashMap<String, Object>();
				mapList.put("timeAll",DateWay.TimeAll());
				mapList.put("courseTime",date);
				List<Map<String, Object>> SlfClassroomCEmployee = myCoursePageMapper.getSlfClassroomCEmployeeToday(mapList);
				//查出教室空余大于2个位置的班级
					if(SlfClassroomCEmployee !=null && SlfClassroomCEmployee.size()>0){
						/**
						 * 主题课不为空  获取一条主题课
						 */
						for (int i = 0;i<SlfClassroomCEmployee.size();i++) {
							mapList.put("id",SlfClassroomCEmployee.get(i).get("id").toString());
							Integer count = myCoursePageMapper.getZjChildrenNumberCount(mapList);
							String a = SlfClassroomCEmployee.get(i).get("classroomNumber").toString();
							Integer b = Integer.parseInt(a);
							if(b > count-2){
								String courseStartTime=SlfClassroomCEmployee.get(i).get("courseStartTime").toString();
								String courseId = SlfClassroomCEmployee.get(i).get("courseId").toString();
								Map<String,Object> map = schoolCoursePageDao.getCourseMessage(Integer.parseInt(courseId));
								DateTime t2 = DateTime.parse(courseStartTime, format);
								Duration duration=new Duration(new DateTime(),t2);
								Long countDown =duration.getStandardSeconds();
								SlfClassroomCEmployee.get(i).put("countDown", countDown);
								map.put("id", SlfClassroomCEmployee.get(i).get("id").toString());
								map.put("name", SlfClassroomCEmployee.get(i).get("courseName").toString());
								map.put("endTime", SlfClassroomCEmployee.get(i).get("endTime").toString());
								map.put("classroomId", SlfClassroomCEmployee.get(i).get("classroomId").toString());
								map.put("classroomName", SlfClassroomCEmployee.get(i).get("classroomName").toString());
								map.put("classroomNumber", SlfClassroomCEmployee.get(i).get("classroomNumber").toString());
								if(ThreadLocalUtil.getUser()!=null) {
								map.put("childrenId",ThreadLocalUtil.getChildren().getChildrenId());
								Integer pd = schoolCoursePageDao.getChildrenCourserCount(map);
								if(pd==0){
								map.put("courseSate",0);
								}else{
								map.put("courseSate",1);
								}
								}else{
								map.put("courseSate",0);
								}
								map.put("course_type",3);
								map.put("dataType", 1);
								Integer totalNumber = myCoursePageMapper.getClassroomNumber(map);
							    Integer alearyNumber = myCoursePageMapper.getZjChildrenNumberCount(map);
							    Integer syTotal = totalNumber - alearyNumber;
							    map.put("syClassroomNumber",syTotal);
								//找到schoolName
								String classroomId = SlfClassroomCEmployee.get(i).get("classroomId").toString();
								map.put("schoolName",slfSchoolMapper.getSchoolNameOther(Integer.parseInt(classroomId)));
								map.put("school_id",SlfClassroomCEmployee.get(i).get("classroomId").toString());
								map.put("course_page_id",SlfClassroomCEmployee.get(i).get("courseId").toString());
								map.put("countDown",SlfClassroomCEmployee.get(i).get("countDown").toString());
								map.put("courseTime",SlfClassroomCEmployee.get(i).get("courseTime").toString());
								resultMapList.add(map);
								break;
						 }
						}
					 List<Map<String,Object>> hotCoursePageList=recentlyMadeReservationsDao.queryHotCoursePage(schoolId,childrenId);
					 if(hotCoursePageList.size()>0){
						 //推荐课程包不为空
						 hotCoursePageList.get(0).put("dataType", 2);
						 hotCoursePageList.get(0).put("schoolName", schoolName);
						 resultMapList.add(hotCoursePageList.get(0));
					 }
					 return resultMapList;
					}else{
						/**
						 * 主题课为空返回两课程包 
						 */
						 List<Map<String,Object>>	hotCoursePageList=recentlyMadeReservationsDao.queryHotCoursePage(schoolId,childrenId);
						if(hotCoursePageList.size()>=2){
							 hotCoursePageList.get(0).put("dataType", 2);
							 hotCoursePageList.get(1).put("dataType", 2);
							 
							 hotCoursePageList.get(0).put("schoolName", schoolName);
							 hotCoursePageList.get(1).put("schoolName", schoolName);
							 
							resultMapList.add(hotCoursePageList.get(0));
							resultMapList.add(hotCoursePageList.get(1));
							
						}else{
							if(hotCoursePageList.size()>0&&hotCoursePageList.size()<2){
								 hotCoursePageList.get(0).put("dataType", 2);
								 hotCoursePageList.get(0).put("schoolName", schoolName);
								resultMapList.add(hotCoursePageList.get(0));
							}
							/**
							 * 查询一个最近B类课程
							 */
							List<Map<String,Object>> courseDataBList= recentlyMadeReservationsDao.queryVacancyCourseDataCorBOrC(1);
							
							Integer residualPosition=0;	
							if(courseDataBList.size()>0 &&courseDataBList!=null){
								//得到一个本校区 在number 表中 缺人 的最近的一条课程数据  
								Map<String,Object> recentlyMadeReservationsCourseData=new HashMap<String,Object>();
								Map<String,Object> courseBData=new HashMap<String,Object>();
								for (int i = 0,b=courseDataBList.size(); i < b; i++) {
									recentlyMadeReservationsCourseData=recentlyMadeReservationsDao.queryCourseDetails(Integer.parseInt(courseDataBList.get(i).get("course_id").toString()),schoolId); 	
									if(recentlyMadeReservationsCourseData!=null&& recentlyMadeReservationsCourseData.size()>0){
										residualPosition=Integer.parseInt(courseDataBList.get(i).get("vacancy").toString());
										courseBData=courseDataBList.get(i);
										break;
									}
								}
								// 查询有空余位置教室信息  
								List<Map<String, Object>> freePositionData = recentlyMadeReservationsDao.queryRoomIdAndEndTime(null);
								if(freePositionData!=null && freePositionData.size()>0){
									Map<String,Object>	 resultMapCourse=new HashMap<String,Object>();
									Integer classcourserId=0;
									Integer vacancyNumber=0;
									Integer classRoomId=0;
									Map<String,Object>freePositionDataCourse=new HashMap<String,Object>();
									for (int i = 0,b=freePositionData.size(); i < b; i++) {
										 classRoomId=Integer.parseInt(freePositionData.get(i).get("classroom_id").toString());
										//排除掉 该堂课已满的教室
										resultMapCourse = recentlyMadeReservationsDao.queryFreePositionCourse(classRoomId,freePositionData.get(i).get("employ_time").toString());
										classcourserId=Integer.parseInt(resultMapCourse.get("course_id").toString());  
										//查询空余教室课程是否在该用户学校下
										freePositionDataCourse=recentlyMadeReservationsDao.queryCourseDetails(Integer.parseInt(courseDataBList.get(i).get("course_id").toString()),schoolId); 	
										//获取该堂课程空余人数
										if(freePositionDataCourse!=null &&freePositionDataCourse.size()>0){
											Integer vacancyCourseNumber	=recentlyMadeReservationsDao.queryVacancyCourseNumber(classcourserId,classRoomId,resultMapCourse.get("employ_time").toString())==null?0:recentlyMadeReservationsDao.queryVacancyCourseNumber(classcourserId,classRoomId,resultMapCourse.get("employ_time").toString());
											Integer classVacancyCourseNumber=Integer.parseInt(freePositionData.get(i).get("employ_num").toString()); 
											if(classVacancyCourseNumber>vacancyCourseNumber){
												vacancyNumber=(classVacancyCourseNumber-vacancyCourseNumber);
												break;
											}
										}
									}
									//教室结束时间
									DateTime t3 = DateTime.parse(resultMapCourse.get("employ_time").toString(), format);
									//class_number
									DateTime t4 = DateTime.parse(courseBData.get("end_time").toString(), format);
									
									if(t4.isBefore(t3)){
										//class_number 时间在前
										recentlyMadeReservationsCourseData.put("freePosition",residualPosition);
										// 日期格式化   开课时间
										DateTime t2 = DateTime.parse(courseBData.get("end_time").toString(), format).minusHours(1);
										recentlyMadeReservationsCourseData=getCourseData(recentlyMadeReservationsCourseData,t2,schoolId);
										recentlyMadeReservationsCourseData.put("classRoomId", courseBData.get("classroom_id"));
										 recentlyMadeReservationsCourseData.put("courseType", 1);
										recentlyMadeReservationsCourseData.put("dataType", 1);
										 String  classRoomName=epOrderServiceDao.queryClassRoom(Integer.parseInt(courseBData.get("classroom_id").toString()));
										 recentlyMadeReservationsCourseData.put("classRoomName", classRoomName);
										
										resultMapList.add(recentlyMadeReservationsCourseData);
									}else{
										//教室那边的时间在前
									freePositionDataCourse.put("freePosition",vacancyNumber);
									// 日期格式化   开课时间
									DateTime t2 = DateTime.parse(resultMapCourse.get("employ_time").toString(), format).minusHours(1);
									freePositionDataCourse=getCourseData(freePositionDataCourse,t2,schoolId);
									freePositionDataCourse.put("classRoomId", classRoomId);
									freePositionDataCourse.put("courseType", 1);
									freePositionDataCourse.put("dataType", 1);
									String  classRoomName=epOrderServiceDao.queryClassRoom(classRoomId);
									freePositionDataCourse.put("classRoomName", classRoomName);
									
									resultMapList.add(freePositionDataCourse);
									}
								}else{
									//class_number 时间在前
									recentlyMadeReservationsCourseData.put("freePosition",residualPosition);
									// 日期格式化   开课时间
									DateTime t2 = DateTime.parse(courseBData.get("end_time").toString(), format).minusHours(1);
									recentlyMadeReservationsCourseData=getCourseData(recentlyMadeReservationsCourseData,t2,schoolId);
									recentlyMadeReservationsCourseData.put("classRoomId", courseBData.get("classroom_id"));
									 recentlyMadeReservationsCourseData.put("courseType", 1);
									recentlyMadeReservationsCourseData.put("dataType", 1);
									 String  classRoomName=epOrderServiceDao.queryClassRoom(Integer.parseInt(courseBData.get("classroom_id").toString()));
									 recentlyMadeReservationsCourseData.put("classRoomName", classRoomName);
									
									resultMapList.add(recentlyMadeReservationsCourseData);
								}
								
							}else{
							
								// 查询有空余位置教室信息  
								List<Map<String, Object>> freePositionData = recentlyMadeReservationsDao.queryRoomIdAndEndTime(null);
								if(freePositionData!=null && freePositionData.size()>0){
									
									Map<String,Object>	 resultMapCourse=new HashMap<String,Object>();
									Integer classcourserId=0;
									Integer vacancyNumber=0;
									Integer classRoomId=0;
									Map<String,Object>freePositionDataCourse=new HashMap<String,Object>();
									for (int i = 0,b=freePositionData.size(); i < b; i++) {
										 classRoomId=Integer.parseInt(freePositionData.get(i).get("classroom_id").toString());
										//排除掉 该堂课已满的教室
										resultMapCourse = recentlyMadeReservationsDao.queryFreePositionCourse(classRoomId,freePositionData.get(i).get("employ_time").toString());
										classcourserId=Integer.parseInt(resultMapCourse.get("course_id").toString());  
										//查询空余教室课程是否在该用户学校下
										freePositionDataCourse=recentlyMadeReservationsDao.queryCourseDetails(Integer.parseInt(courseDataBList.get(i).get("course_id").toString()),schoolId); 	
										//获取该堂课程空余人数
										if(freePositionDataCourse!=null &&freePositionDataCourse.size()>0){
											Integer vacancyCourseNumber	=recentlyMadeReservationsDao.queryVacancyCourseNumber(classcourserId,classRoomId,resultMapCourse.get("employ_time").toString());
											Integer classVacancyCourseNumber=Integer.parseInt(freePositionData.get(i).get("employ_num").toString()); 
											if(classVacancyCourseNumber>vacancyCourseNumber){
												vacancyNumber=(classVacancyCourseNumber-vacancyCourseNumber);
												break;
											}
										}
									}	
									
									//教室那边的时间在前
									freePositionDataCourse.put("freePosition",vacancyNumber);
									// 日期格式化   开课时间
									DateTime t2 = DateTime.parse(resultMapCourse.get("employ_time").toString(), format).minusHours(1);
									freePositionDataCourse=getCourseData(freePositionDataCourse,t2,schoolId);
									freePositionDataCourse.put("classRoomId", classRoomId);
									freePositionDataCourse.put("courseType", 1);
									freePositionDataCourse.put("dataType", 1);
									String  classRoomName=epOrderServiceDao.queryClassRoom(classRoomId);
									freePositionDataCourse.put("classRoomName", classRoomName);
									resultMapList.add(freePositionDataCourse);
									
								}
							}
						}
						return resultMapList;
					}
					
			}
		}
		
	}
	
	/**
	 *   获取课程数据
	 * @param courseData  返回map数据
	 * @param t2  开课时间
	 * @param schoolId  学校ID
	 * @return
	 */
	private Map<String,Object> getCourseData(Map<String,Object> courseData,DateTime t2,Integer schoolId){
		
			Duration duration=new Duration(new DateTime(),t2);
			Long countDown =duration.getStandardSeconds();
			String shcoolCourseId =courseData.get("school_course_id").toString();
			//查询学校课程包ID
			Map<String,Object> schoolCoursetoPageData=recentlyMadeReservationsDao.querySchoolCoursePageId(Integer.parseInt(shcoolCourseId), schoolId);
			courseData.put("countDown", countDown);
			courseData.put("school_course_page_id",schoolCoursetoPageData.get("school_course_page_id"));
			//1 立即排队
			courseData.put("courseSate", "1");
			courseData.put("dataType", "1");
			if(courseData.get("sales_start_date")!=null && courseData.get("sales_start_date")!=""){
				String  str=courseData.get("sales_start_date").toString();
				courseData.put("sales_start_date", new DateTime(str).toString("yyyy-MM-dd")); 
			}
			if(courseData.get("sales_end_date")!=null && courseData.get("sales_end_date")!=""){
				String  str=courseData.get("sales_end_date").toString();
				courseData.put("sales_end_date", new DateTime(str).toString("yyyy-MM-dd")); 
			}
			
			return courseData;
	}
}
